package com.atguigu.bigdata.scala.chapter07

object Scala12_Collection_WordCount_Test_1 {

    def main(args: Array[String]): Unit = {

        // TODO - WordCount
        val list = List(
            ("hello", 4),
            ("hello spark", 3),
            ("hello spark scala", 2),
            ("hello spark scala hive", 1)
        )
        // TODO 第二种实现方式
        // 想要的结果为wordcount => (Hello, 10)
        // 发现集合种的元素其实就是wordcount,只是word是多个
        /*

        ("hello spark", 3) => (hello, 3), (spark, 3)
        ("hello spark scala", 2) => (hello, 2), (spark, 2), (scala, 2)

        hello => List( (hello, 3), (hello, 2), (hello, 1) )

        =>

        hello => List( 3, 2, 1 )

        =>

        (hello, 6)

         */

        val newlist = list.flatMap(
            t => {
                val str = t._1
                val cnt = t._2
                val words = str.split(" ")
                words.map(
                    word => {
                        (word, cnt)
                    }
                )
            }
        )

        // 根据单词对数据进行分组
        //newlist.groupBy((t)=>t._1)
        val groups = newlist.groupBy(_._1)

        // word => List(t1, t2, t3)
        // word => List(3, 2, 1)
        // word => 1

        // 在scala集合种，KV类型的数据，我们不想对Key做处理，只想对Value进行处理，那么可以采用特殊的方法：
        // TODO mapValues
//        val wordSum = groups.map(
//            t => {
//                val word = t._1
//                val list = t._2
//                val sum = list.map(kv=>kv._2).sum
//                (word, sum)
//            }
//        )
        val wordSum = groups.mapValues(
            list => {
                list.map(_._2).sum
            }
        )

        println(wordSum)
    }
}
